package com.landary.parameterrplacement.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.*;

/**
 * @author 刘欣
 * @createTime 2023/8/17 14:52
 * @description 达梦数据库连接
 */
@Component
@Slf4j
public class JDBCUtils {

    //机密机正式数据库
    private static final String URL = "jdbc:dm://192.9.195.76:5239";
    private static final String USERNAME = "mkddm";
    private static final String PASSWORD = "abcABC123";
    //客户环境机密服务器数据库
//    private static final String URL = "jdbc:dm://192.9.195.73:5237";
//    private static final String USERNAME = "MKPAAS";
//    private static final String PASSWORD = "abcABC123";
    //客户测试环境达梦数据库
//    private static final String URL = "jdbc:dm://192.9.170.40:5237";
//    private static final String USERNAME = "mkpaas";
//    private static final String PASSWORD = "abcABC123";
    //公司测试环境数据库
//    private static final String URL = "jdbc:dm://192.168.0.221:5237";
//    private static final String USERNAME = "mkpaas";
//    private static final String PASSWORD = "abcABC123";
    //本地达梦数据库
//    private static final String URL = "jdbc:dm://localhost:5236";
//    private static final String USERNAME = "SYSDBA";
//    private static final String PASSWORD = "SYSDBA";

    //本地mysql
//    private static final String URL = "jdbc:mysql://localhost:3306/sf601_product?useSSL=false&serverTimezone=UTC";
//    private static final String USERNAME = "root";
//    private static final String PASSWORD = "root";
    //客户测试环境mysql
//    private static final String URL = "jdbc:mysql://192.9.170.35:3306/mkpaas?useSSL=false&serverTimezone=UTC";
//    private static final String USERNAME = "root";
//    private static final String PASSWORD = "abcABC123";

    public Connection getConnection() throws SQLException, ClassNotFoundException {
        log.info("获取数据库连接");
        Class.forName("dm.jdbc.driver.DmDriver");
//        Class.forName("com.mysql.cj.jdbc.Driver");
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
